package com.xjy.rpc.utils;

import com.xjy.rpc.annotation.PropertiesField;
import com.xjy.rpc.annotation.PropertiesPrefix;
import com.xjy.rpc.config.Constant;
import org.springframework.core.env.Environment;

import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;

/**
 * @description: 读取配置文件的工具包
 * @author XJY
 * @date 2024/10/18 9:36
 * @version 1.0
 */
public class PropertiesUtils {

    /**
     * 根据对象中的配置匹配配置文件
     * @param o
     * @param environment
     */
    public static void init(Object o, Environment environment){
        final Class<?> aClass = o.getClass();
        // 获取前缀
        final PropertiesPrefix prefixAnnotation = aClass.getAnnotation(PropertiesPrefix.class);
        if (prefixAnnotation == null){
            throw new NullPointerException(aClass + " @PropertiesPrefix 不存在");
        }
        String prefix = prefixAnnotation.value();
        // 前缀参数矫正
        if (!prefix.contains(".")){
            prefix += ".";
        }
        // 遍历对象中的字段
        for (Field field : aClass.getDeclaredFields()) {
            final PropertiesField fieldAnnotation = field.getAnnotation(PropertiesField.class);
            if (fieldAnnotation == null) continue;;
            String fieldValue = fieldAnnotation.value();
            if(fieldValue == null || fieldValue.equals("")){
                fieldValue = convertToHyphenCase(field.getName());
            }
            try {
                field.setAccessible(true);
                final Class<?> type = field.getType();
                final Object value = PropertyUtils.handle(environment, prefix + fieldValue, type);
                if(value == null) continue;
                //处理过期时间
                if (Constant.TIME_UNIT.equals(hyphenCaseToConvert(fieldValue))) {
                    String timeUnit = getTimeUnitEnumByConfig((String) value);
                    field.set(o,timeUnit);
                    continue;
                }
                field.set(o, value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            field.setAccessible(false);
        }
    }


    //驼峰转-
    public static String convertToHyphenCase(String input) {
        StringBuilder output = new StringBuilder();

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (Character.isUpperCase(c)) {
                output.append('-');
                output.append(Character.toLowerCase(c));
            } else {
                output.append(c);
            }
        }

        return output.toString();
    }


    //-转驼峰 expire-time -> expireTime
    public static String hyphenCaseToConvert(String input) {
        StringBuilder output = new StringBuilder();

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == '-') {
                i++;
                output.append(Character.toUpperCase(input.charAt(i)));
            } else {
                output.append(c);
            }
        }

        return output.toString();
    }

    //配置文件中配的过期时间转为枚举 second -> SECONDS
    public static String getTimeUnitEnumByConfig(String timeUnit){
        StringBuilder builder = new StringBuilder(timeUnit);
        builder.append("s");
        return builder.toString().toUpperCase();
    }

    public static void main(String[] args) {
        String s = convertToHyphenCase("springBootConfig");
        String s1 = hyphenCaseToConvert("expire-time");
        String second1 = getTimeUnitEnumByConfig("second");
        System.out.println(s);
        System.out.println(s1);
        System.out.println(second1);
        TimeUnit second = TimeUnit.valueOf("SECONDS");
        long toMinutes = second.toMinutes(3600);
        System.out.println(toMinutes);
    }
}
